package com.edu.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet Filter implementation class UsersLoginFilter
 */
@WebFilter(urlPatterns= {"/front/blog-single.jsp","/front/contact.jsp","/front/privacy-policy.jsp","/front/aritcle-list.jsp"})
public class UsersLoginFilter implements Filter {

	/**
	 * Default constructor. 
	 */
	public UsersLoginFilter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
		//System.out.println("拦截Login");
		HttpServletRequest request=(HttpServletRequest)req;
		HttpServletResponse response=(HttpServletResponse)res;
		String url=request.getServletPath();
		System.out.println(url);
		response.setContentType("text/html");
		HttpSession session=request.getSession();
		if(session.getAttribute("frontUser")!=null) {
			chain.doFilter(request, response);
		}else {
			response.getWriter().print("<script>alert('请登录');location.href='index.jsp'</script>");
		}
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
